(function(){
	'use strict';

	var GRID = angular.module('zhxForecastGridModule', []);
	
	GRID.controller('zhxForecastGridCtrl', ['$scope','$q',function($scope,$q){

		var gridReady = $q.defer();

		$scope.gridColumn = $scope.options.columns;

		gridReady.promise.then(function(alarmData){
			$scope.gridData = alarmData;
		});

		this.getOptions = function(){
			return $scope.options;
		};

		$scope.$watch('options.data',function(nv,ov){
			if( nv !== ov && nv ){
				gridReady.resolve( nv );
			}
		});

		$scope.gridFilter = 'test';

	}]);

	GRID.directive('zhxCtcGrid', function(){
		return{
			restrict : 'E',
			scope: {
				options : '='
			},
			replace: true,
			transclude: true,
			controller : 'zhxForecastGridCtrl',
			link : function( scope, tElement, tAttrs ){

				var body = tElement.find('.zhx-forecast-body'),
					header = tElement.find('.zhx-forecast-header');

				body.on('scroll',function(event){
					var left = angular.element(event.target).scrollLeft();
					header.css('left', -left );
				});

				//打开grid过滤弹出层
				var filterShown = false;
				scope.showFilter = function(event){
					var ev = event || window.event,
						target = angular.element( ev.target );
					if( !filterShown ){
						target.velocity({ height : 60  }, {
							easing : 'ease',
							duration : 200
						} );
						filterShown = !filterShown;
					} else {
						target.velocity('reverse');
					}
				};

			},
			templateUrl : 'zhx.ctc.forecastGrid.vicco'
		}

	});

	//row
	GRID.directive('zhxForecastRow', function(){
		return{
			restrict : 'E',
			scope : true,
			require : '^zhxCtcGrid',
			replace: true,
			link : function( scope, tElement, tAttrs, zhxCtcGrid ){
				//wrapper scope
				// var scp = scope.$parent;
				//process status
				// scope.isProcessed = false;

				//process event
				scope.processed = function(){
					console.log('grid processed.')
					// scope.isProcessed = true;
				};

				//event bind
				scope.processForecast = function(event, rowData){
					var ev = event || window.event;
					var opts = zhxCtcGrid.getOptions();
					try{
						opts.event.call( this, scope, rowData, ev);
					}catch(e){
						//do not init options event key
						console.warn('请在options中设置event')
					}
				};

			},
			templateUrl : 'zhx.ctc.forecastRow.vicco'
		}
	});
	//cell
	GRID.directive('zhxForecastCell', ['$filter',function($filter){
		return{
			restrict:'E',
			scope: {
				row : '=',
				column : '='
			},
			replace: true,
			link : function( scope, tElement, tAttrs ){
				var row = scope.row,
					column = scope.column;

				if( !angular.isUndefined(scope.column) ){

					var filterType = typeof( column.filter );

                    switch (filterType) {
                        case 'string':
                            var filterHtml = $filter(column.filter)(row[column.name]);
                            row[column.name] = filterHtml;
                            break;
                        case 'object':
                            var filter = column.filter;
                            var filterHtml = $filter(filter.name)(row[column.name], filter.params[0], filter.params[1]);
                             row[column.name] = filterHtml;
                            break;
                    }
				}


			},
			templateUrl : 'zhx.ctc.forecastCell.vicco'
		}
	}]);

})()